﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace InternetBankService.Model
{
    using InternetBankService.Data;
    using System.Web.Security;
    using HelpersAndContracts.Contracts;

    using InternetBankService.Helpers;

    public class Repository : IRepository
    {
        private readonly BankInfoDataContext dataContext;

        private readonly IHashClaculator hashClaculator;

        private Random random = new Random();

        public Repository(BankInfoDataContext dataContext, IHashClaculator hashClaculator)
        {
            this.dataContext = dataContext;
            this.hashClaculator = hashClaculator;
        }

        public Client GetClientByUsername(string username)
        {
            return dataContext.Clients.FirstOrDefault(x => string.Equals(x.Username, username));
        }

        public void AddPayment(PaymentOnHold paymentOnHold)
        {
            var payment = new Payment {
                    Amount = Int32.Parse(paymentOnHold.Amont),
                    Date = DateTime.Now,
                    Recipient = paymentOnHold.Recipient,
                    Username = paymentOnHold.Username,
                    Number = paymentOnHold.Number
                };
            dataContext.Payments.InsertOnSubmit(payment);
            dataContext.SubmitChanges();
        }

        public IEnumerable<Payment> GetUserPayments(string username)
        {
            return this.dataContext.Payments.Where(x => x.Username.Equals(username));
        }

        public ClientModel AddClient(ClientModel client)
        {
            client.Password = Membership.GeneratePassword(10, 0);
            client.TransactionPassword = Membership.GeneratePassword(10, 0);
            client.CardId = string.Format("{0} {1} {2} {3}", random.Next(1000, 9999), random.Next(1000, 9999), random.Next(1000, 9999), random.Next(1000, 9999)); 
            client.Username = string.Format("{0}_{1}", client.Name.ToLower(), client.Surname.ToLower());

            var existingUser = dataContext.Clients.Count(x => x.Username.StartsWith(client.Username));
            if (existingUser > 0)
            {
                client.Username = client.Username + existingUser;
            }

            dataContext.Clients.InsertOnSubmit(new Client
                {
                    Balance = random.Next(1000000,9999999),
                    CardId = client.CardId,
                    DateOfBirth = DateTime.Parse(client.DateOfBirth),
                    Email = client.Email,
                    Name = client.Name,
                    NumberOfPassport = client.NumberOfPassport,
                    Password = hashClaculator.CalculateHash(client.Password),
                    Patronymic = client.Patronymic,
                    Phone = client.Phone,
                    Surname = client.Surname,
                    SeriesofPassport = client.SeriesofPassport,
                    TransactionPassword = hashClaculator.CalculateHash(client.TransactionPassword),
                    Username = client.Username
                });
            dataContext.SubmitChanges();

            return client;
        }

        public bool PaymentExist(PaymentModel payment)
        {
            return dataContext.Payments.Any(x => string.Equals(x.Number, payment.Number) && string.Equals(x.Username, payment.Username));
        }
    }
}